﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using System.Reflection;
using System.Diagnostics;

namespace ImbedLibrary
{
    /// <summary>
    /// 日志记录类
    /// </summary>
    public class Logs
    {

        /// <summary>
        /// 获取记录文件路径
        /// </summary>
        /// <returns>日志文件路径</returns>
        private static String Path()
        {
            String logPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\Logs\";
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }
            return logPath;
        }

        /// <summary>
        /// 错误信息记录
        /// </summary>
        /// <param name="msg">记录的内容</param>
        public static void Err(String msg)
        {
            String logPath = Path() + "err.log";
            Note(logPath, msg, true);
        }

        /// <summary>
        /// 记录警告信息
        /// </summary>
        /// <param name="msg">内容</param>
        public static void Warning(string msg)
        {
            Note(Path() + "Warning.log",msg,true);
        }

        /// <summary>
        /// 错误信息记录
        /// </summary>
        /// <param name="msg">记录的内容</param>
        /// <param name="name">文件名字</param>
        public static void Err(String msg, string name)
        {
            string path = Path() + DateTime.Now.ToString("yyyyMMdd") + "[" + name + "][err].log";
            Note(path, msg, true);
        }

        /// <summary>
        /// 错误信息记录
        /// </summary>
        /// <param name="method">方法</param>
        /// <param name="param">参数</param>
        /// <param name="discription">描述</param>
        public static void Err(String method, String param ,String discription)
        {
            String message = "方法: " + method + "\r\n参数:" + param + "\r\n描述:" + discription;
            Err(message);
        }

        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="msg">记录的内容</param>
        public static void Note(String msg)
        {
            //Console.WriteLine(msg);
            String logPath = Path() + "note.log";
            Note(logPath, msg, true);
        }

        /// <summary>
        /// 日志记录
        /// 日志自动记录调用者的名字
        /// </summary>
        /// <param name="msg">记录的信息</param>
        public static void MethodNote(string msg)
        {
            StackTrace trace = new StackTrace();
            MethodBase metbodBase = trace.GetFrame(1).GetMethod();
            Note(Path() + "note.log", metbodBase.Name + ":" + msg);
        }

        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="path">日志文件路径</param>
        /// <param name="msg">内容</param>
        public static void Note(string path, string msg)
        {
            Note(path, msg, false);
        }

        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="msg">记录的内容</param>
        /// <param name="path">日志文件路径</param>
        /// <param name="format">是否格式化信息</param>
        public static void Note(String path, String msg, bool format)
        {
            if (path.IndexOf('\\') == -1)
                path = Path() + DateTime.Now.ToString("yyyyMMdd") + "[" + path + "][note].log";
            if (!File.Exists(path))
            {
                FileStream fs = File.Create(path);
                fs.Dispose();
                fs.Close();
            }
            using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Write))
            {
                StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                if (format) sw.WriteLine(DateTime.Now + "  " + msg);
                else sw.WriteLine(msg);
                sw.Dispose();
                sw.Close();
            }
        }

    }
}
